package gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import basic.Country;
import basic.League;
import kontroler.Kontroler;

@SuppressWarnings("serial")
public class CompetitionSplitPane extends JSplitPane{

	private JScrollPane countryPanel = null;
	private JScrollPane leaguePanel = null;
	private JList countryList = null;
	private JList leagueList = null;
	private DefaultListModel countryListModel = null;
	private DefaultListModel leagueListModel = null;
	private JPopupMenu countryPopUpMenu = null;
	private JPopupMenu leaguePopUpMenu = null;
	private JMenuItem addCountry = null;
	private JMenuItem removeCountry = null;
	private JMenuItem close = null;
	private JMenuItem close2 = null;
	private JMenuItem addLeague = null;
	private JMenuItem removeLeague = null;
	private Kontroler kontroler = null;

	public CompetitionSplitPane(Kontroler kontroler) {
		super(JSplitPane.HORIZONTAL_SPLIT);
		this.setLayout(new FlowLayout());
		this.setPreferredSize(new Dimension(300, 400));
		this.add(getCountryPanel());
		this.add(getLeaguePanel());
		this.setDividerLocation(130);
		this.kontroler = kontroler;
	}
	private JScrollPane getCountryPanel() {
		if (countryPanel == null) {
			countryPanel = new JScrollPane();
			countryPanel.setViewportView(getCountryList());
		}
		return countryPanel;
	}
	private JScrollPane getLeaguePanel() {
		if (leaguePanel == null) {
			leaguePanel = new JScrollPane();
			leaguePanel.setViewportView(getLeagueList());
		}
		return leaguePanel;
	}
	private JList getCountryList() {
		if (countryList == null) {
			countryList = new JList(getCountryListModel());
			countryList.setFont(new Font("Algerian", Font.PLAIN, 14));
			countryList.addMouseListener(new MouseListener() {

				@Override
				public void mouseReleased(MouseEvent arg0) {
					if (arg0.getButton() == 3){
						getCountryPopUpMenu();
						if (getLeaguePopUpMenu().isVisible()){
							getLeaguePopUpMenu().setVisible(false);
						}
						getCountryPopUpMenu().setVisible(true);
						getCountryPopUpMenu().show(null, arg0.getXOnScreen(), arg0.getYOnScreen());

					}

				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					if (arg0.getButton() == MouseEvent.BUTTON1){
						if ((String)countryList.getSelectedValue() != null){
							populateLeaguesList();
						}
					}
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseClicked(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}	
			});
			//countryList.ad
		}
		return countryList;
	}
	private JList getLeagueList() {
		if (leagueList == null) {
			leagueList = new JList(getLeagueListModel());
			leagueList.setFont(new Font("Algerian", Font.PLAIN, 14));
			leagueList.addMouseListener(new MouseListener() {

				@Override
				public void mouseReleased(MouseEvent arg0) {
					if (arg0.getButton() == 3) {
						getLeaguePopUpMenu();
						if (getCountryPopUpMenu().isVisible()){
							getCountryPopUpMenu().setVisible(false);
						}
						getLeaguePopUpMenu().setVisible(true);
						getLeaguePopUpMenu().show(null, arg0.getXOnScreen(), arg0.getYOnScreen());
					}

				}

				@Override
				public void mousePressed(MouseEvent arg0) {

				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseClicked(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}
			});
		}
		return leagueList;
	}
	private DefaultListModel getCountryListModel() {
		if (countryListModel == null) {
			countryListModel = new DefaultListModel();
		}
		return countryListModel;
	}
	private DefaultListModel getLeagueListModel() {
		if (leagueListModel == null) {
			leagueListModel = new DefaultListModel();
		}
		return leagueListModel;
	}
	private JPopupMenu getCountryPopUpMenu(){
		if (countryPopUpMenu == null) {
			countryPopUpMenu = new JPopupMenu();
			countryPopUpMenu.add(getAddCountry());
			countryPopUpMenu.add(getRemoveCountry());
			countryPopUpMenu.addSeparator();
			countryPopUpMenu.add(getClose());
		}
		return countryPopUpMenu;
	}
	private JPopupMenu getLeaguePopUpMenu(){
		if (leaguePopUpMenu == null) {
			leaguePopUpMenu = new JPopupMenu();
			leaguePopUpMenu.add(getAddLeague());
			leaguePopUpMenu.add(getRemoveLeague());
			leaguePopUpMenu.addSeparator();
			leaguePopUpMenu.add(getClose2());
		}
		return leaguePopUpMenu;
	}
	private JMenuItem getAddCountry(){
		if (addCountry == null) {
			addCountry = new JMenuItem("Add Country");
			addCountry.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					countryPopUpMenu.setVisible(false);
					String country = JOptionPane.showInputDialog(null, "Enter country name", "Add Country", JOptionPane.OK_CANCEL_OPTION);
					kontroler.addCountry(country);
					populateCountriesList();
				}
			});
		}
		return addCountry;
	}
	private JMenuItem getRemoveCountry() {
		if (removeCountry == null) {
			removeCountry = new JMenuItem("Remove Country");
			removeCountry.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					countryPopUpMenu.setVisible(false);
					kontroler.removeCountry((String)countryList.getSelectedValue());
					//countryListModel.removeElement((String)countryList.getSelectedValue());
					populateCountriesList();
				}
			});
		}
		return removeCountry;
	}
	private JMenuItem getClose() {
		if (close == null) {
			close = new JMenuItem("Close");
			close.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					countryPopUpMenu.setVisible(false);
				}
			});

		}
		return close;
	}
	private JMenuItem getClose2() {
		if (close2 == null) {
			close2 = new JMenuItem("Close");
			close2.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					leaguePopUpMenu.setVisible(false);
				}
			});
		}
		return close2;
	}
	private JMenuItem getAddLeague() {
		if (addLeague == null) {
			addLeague = new JMenuItem("Add League");
			addLeague.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					leaguePopUpMenu.setVisible(false);
					if (countryList.getSelectedValue() != null){
						String country = (String)countryList.getSelectedValue();
						String league = JOptionPane.showInputDialog(null,"Country: "+country+"\nEnter league name","Add League",JOptionPane.OK_CANCEL_OPTION);
						kontroler.addLeague(league, kontroler.getCountry(country));
						populateLeaguesList();
					} else {
						JOptionPane.showMessageDialog(null, "Select a country first");
					}

				}
			});
		}
		return addLeague;
	}
	private JMenuItem getRemoveLeague() {
		if (removeLeague == null) {
			removeLeague = new JMenuItem("Remove League");
			removeLeague.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					leaguePopUpMenu.setVisible(false);
					kontroler.removeLeague((String)leagueList.getSelectedValue(),kontroler.getCountry((String)countryList.getSelectedValue()));
					//leagueListModel.removeElement((String)countryList.getSelectedValue());
					populateLeaguesList();

				}
			});
		}
		return removeLeague;
	}
	private void populateCountriesList(){
		countryListModel.removeAllElements();
		Country[] countries = kontroler.getCountries();
		for (int i = 0; i < countries.length; i++){
			countryListModel.addElement(countries[i].getName());
		}
	}
	private void populateLeaguesList(){
		leagueListModel.removeAllElements();
		League[] leagues = kontroler.getLeagues(kontroler.getCountry((String)countryList.getSelectedValue()));
		for(int i = 0; i < leagues.length; i++){
			leagueListModel.addElement(leagues[i].getName());
		}
	}
	public Country getSelectedCountry(){
		if (countryList.getSelectedValue() != null){
			return kontroler.getCountry((String)countryList.getSelectedValue());
		}
		JOptionPane.showMessageDialog(null, "Select a country.");
		return null;
	}
	public League getSelectedLeague(){ 
		String name = (String) leagueList.getSelectedValue();
		Country country = kontroler.getCountry((String)countryList.getSelectedValue());
		if (name != null && country != null){
			return kontroler.getLeague(name, country);
		}
		return null;
	}
	public String getSelectedCountryValue(){
		if (countryList.getSelectedValue() != null){
			return (String)countryList.getSelectedValue();
		} else {
			JOptionPane.showMessageDialog(null, "Country not selected");
			return "";
		}
	}

}
